#include "datahandle.h"
#include <QProcess>
#include <QFile>
#include <QSettings>
#include <QDir>
#include <QDebug>
#include <QApplication>
#include <QDateTime>

unsigned short int CheckCRC_16(const unsigned char *data, const unsigned int sIndex, const unsigned int length)
{
    unsigned int CRCreg=0xFFFF;
    int len=length+sIndex;
    for(int i=sIndex;i<len;i++)
    {
        CRCreg=CRCreg^data[i];
        for(int j=0;j<=7;j++)
        {
            if(CRCreg&0x01)
            {
                CRCreg=(CRCreg>>1)^0xa001;
            }
            else
            {
                CRCreg=CRCreg>>1;
            }
        }
    }
    return CRCreg;
}

unsigned short CheckCRC_16(const QByteArray &dataByte, const unsigned int sIndex, const unsigned int length)
{
    unsigned char *data=(unsigned char *)dataByte.data();
    unsigned int CRCreg=0xFFFF;
    int len=length+sIndex;
    for(int i=sIndex;i<len;i++)
    {
        CRCreg=CRCreg^data[i];
        for(int j=0;j<=7;j++)
        {
            if(CRCreg&0x01)
            {
                CRCreg=(CRCreg>>1)^0xa001;
            }
            else
            {
                CRCreg=CRCreg>>1;
            }
        }

    }
    return CRCreg;
}

void CheckCRC_16_add(QByteArray &dataByte, const unsigned int sIndex, const unsigned int length)
{
    unsigned RetCRC=CheckCRC_16(dataByte,sIndex,length);
    dataByte.append(RetCRC&0x00ff);
    dataByte.append((RetCRC&0xff00)>>8);
}
bool CheckCRC_16_IsOk(const unsigned char *data, const unsigned int sIndex, const unsigned int length)
{
    if(length<3){ return  false; }
    unsigned int RetCrc=CheckCRC_16(data,sIndex,length-2);
    unsigned char Crc[2];
    Crc[0]=data[sIndex+length-2];
    Crc[1]=data[sIndex+length-1];
    if((RetCrc&0x00FF)!=Crc[0]||((RetCrc&0xFF00)>>8)!=Crc[1]){
        return false;
    }else{
        return true;
    }
    return true;
}
bool CheckCRC_16_IsOk(const QByteArray &dataByte, const unsigned int sIndex, const unsigned int length)
{
    if(dataByte.count()<3){ return  false; }
    unsigned int RetCrc=CheckCRC_16(dataByte,sIndex,length-2);
    unsigned char Crc[2];
    Crc[0]=dataByte[sIndex+length-2];
    Crc[1]=dataByte[sIndex+length-1];
    if((RetCrc&0x00FF)!=Crc[0]||((RetCrc&0xFF00)>>8)!=Crc[1]){
        return false;
    }else{
        return true;
    }
    return true;
}

unsigned short int CheckSum_8(const unsigned char *data, const unsigned int sIndex, const unsigned int length)
{
    unsigned short int CRCreg=0x0000;
    int len=length+sIndex;
    for(int i=sIndex;i<len;i++)
    {
        CRCreg+=data[i];
    }
    return CRCreg&0x00FF;
}
unsigned short CheckSum_8(const QByteArray &dataByte, const unsigned int sIndex, const unsigned int length)
{
    unsigned char *data=(unsigned char *)dataByte.data();
    unsigned short int CRCreg=0x0000;
    int len=length+sIndex;
    for(int i=sIndex;i<len;i++)
    {
        CRCreg+=data[i];
    }
    return CRCreg&0x00FF;
}

QVector<QByteArray> QByteArraySplit(const QByteArray &SrcByteArray,const int Count)
{
    QVector<QByteArray> RetByte;
    unsigned int Benindex=0;
    while(1)
    {
        RetByte.append(SrcByteArray.mid(Benindex,Count));
        Benindex+=Count;
        if(RetByte.last().count()<Count)
        {
            RetByte.removeAt(RetByte.count()-1);
            break;
        }
    }
    return RetByte;
}

QList<QByteArray> QByteArraySplitChar(const QByteArray &SrcByteArray,const QByteArray Split)
{
    QList<QByteArray> RetByte;
    int Benindex=0;
    int EndIndex=0;
    while(1)
    {
        EndIndex=SrcByteArray.indexOf(Split,Benindex);
        //qDebug()<<"EndIndex"<<EndIndex;
        if(EndIndex!=-1){
            RetByte.append(SrcByteArray.mid(Benindex,EndIndex-Benindex+Split.count()));
        }else{
            RetByte.append(SrcByteArray.mid(Benindex,-1));
            break;
        }
        Benindex=EndIndex+Split.count();
    }
    return RetByte;
}
